/**
 * Created by zhangPeng on 2017/1/13.
 */

app.controller('sysMonitorCtrl', function ($scope, sysMonitorService, $timeout) {

    var intervalTime = 30000;

    /**
     * js中百分数转小数
     * @param percent
     * @returns {number}
     */
    function toPoint(percent) {
        return Number(percent.replace('%', '')) / 100;
    }


    /**
     * js中小数转百分数
     * @returns {string}
     */
    Number.prototype.toPercent = function () {
        return (Math.round(this * 10000) / 100).toFixed(2) + '%';
    }

    var date = [];
    var data1 = [];
    var data2 = [];

    /**
     * GW网络折线图X轴数据
     * @param shift
     */
    function addNetworkDate() {
        intervalTime = $scope.gwTime;
        var today = new Date();
        for(var i = 4; i >= 1; i--){
            var intervalDate = new Date(today.getTime() - $scope.gwTime * i );
            date.push(intervalDate.getHours() + ":" + intervalDate.getMinutes() + ":" + intervalDate.getSeconds());
        }
        date.push(today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds());
    }

    /**
     * 刷新时间
     * @param time
     */
    $scope.changeTime = function(time){
        $scope.gwTime=time;
        intervalTime = time;
    }

    /**
     * GW系统监控
     */
    var gwChart1 = echarts.init(document.getElementById('main1'));
    var gwChart2 = echarts.init(document.getElementById('main2'));
    var gwChart3 = echarts.init(document.getElementById('main3'));
    var gwChart4 = echarts.init(document.getElementById('main4'));
    var gwChart5 = echarts.init(document.getElementById('main5'));
    function getGWSystemService() {
        sysMonitorService.getGWSystemService({
            action: 'getGWSystemService'
        }, function (data) {
            var homeDiskArrays = new Array();
            var rootDiskArrays = new Array();
            var memArrays = new Array();
            addNetworkDate();
            if (data.message != null) {
                if( 'undefined' != typeof(data.message.CPU)){
                    var cpuJson = JSON.parse(data.message.CPU.message);
                    $scope.GWcpuCombined = cpuJson[0].combined;
                }
                else{
                    $scope.GWcpuCombined = 0;
                }

                if( 'undefined' != typeof(data.message.Memory)){
                    var GWmemUserd =  JSON.parse(data.message.Memory.message).used.toFixed(2);
                    var GWmemFree = JSON.parse(data.message.Memory.message).free.toFixed(2);
                    memArrays[0] ={
                        value : GWmemUserd,
                        name : '已用'
                    }
                    memArrays[1] ={
                        value : GWmemFree,
                        name : '空闲'
                    }
                }
                else{
                    memArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                }
                if( 'undefined' != typeof(data.message.Disk)){
                    var diskObj = JSON.parse(data.message.Disk.message);
                    for(var key in diskObj){
                        if(key == '/home'){
                            homeDiskArrays[0] ={
                                value : diskObj[key],
                                name : '已用'
                            }
                            homeDiskArrays[1] ={
                                value : 100 - diskObj[key],
                                name : '空闲'
                            }
                        }
                        if(key == '/'){
                            rootDiskArrays[0] ={
                                value : diskObj[key],
                                name : '已用'
                            }
                            rootDiskArrays[1] ={
                                value : 100 - diskObj[key],
                                name : '空闲'
                            }
                        }
                    }
                }
                else{
                    homeDiskArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                    rootDiskArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                }

                if( 'undefined' != typeof(data.message.Network)){
                    var rxJson = JSON.parse(data.message.Network.message).rx;
                    var txJson = JSON.parse(data.message.Network.message).tx;
                    for(var k=0; k<rxJson.length; k++){
                        data1.push((rxJson[k] /1024 ).toFixed(4));
                        data2.push((txJson[k] /1024 ).toFixed(4));
                    }
                }
                else{
                    for(var k=0; k<5; k++){
                        data1.push(0);
                        data2.push(0);
                    }
                }
            } else {
                $scope.GWcpuCombined = 0;
                memArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                homeDiskArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                rootDiskArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                for(var k=0; k<5; k++){
                    data1.push(0);
                    data2.push(0);
                }
            }

            var option = {
                title: {
                    text: '网络监控',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder'
                    }
                },
                //设置图表大小
                grid: {
                    x: 130,
                    y: 60
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: date
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        //animation: false
                    }
                },
                yAxis: {
                    boundaryGap: [0, '80%'],
                    axisLabel: {
                        formatter: '{value}（KB）'
                    },
                    splitNumber: 5
                },
                series: [

                    {
                        name: '接收',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: data1
                    },
                    {
                        name: '发送',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: data2
                    }
                ]
            };
            var option1 = {
                title: {
                    text: 'CPU',
                    x: '39%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    feature: {}
                },
                series: [
                    {
                        name: 'CPU指标',
                        type: 'gauge',
                        //占用比例样式
                        detail: {
                            formatter: '{value}%',
                            textStyle: {
                                color: 'auto',
                                fontSize: 15
                            }
                        },
                        data: [{
                            value: Math.round($scope.GWcpuCombined),
                            name: '占用率'
                        }
                        ],
                        //坐标标签样式
                        axisLabel: {
                            show: false,
                            formatter: null,
                            textStyle: {
                                color: 'auto'
                            }
                        },

                        //指针
                        pointer: {
                            length: '70%',
                            width: 4,
                            color: 'auto'
                        }
                    }
                ]
            };
            var option2 = {
                title: {
                    text: '内存',
                    //subtext: '纯属虚构',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: memArrays
                },
                series: [
                    {
                        name: '内存容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: memArrays
                    }
                ]
            };
            var option3 = {
                title: {
                    text: '硬盘(/home)',
                    x: '15%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{c}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: homeDiskArrays
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: homeDiskArrays
                    }
                ]
            };
            var option5 = {
                title: {
                    text: '硬盘(/)',
                    x: '30%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{c}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: rootDiskArrays
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: rootDiskArrays
                    }
                ]
            };
            // 为echarts对象加载数据
            gwChart1.setOption(option1);
            gwChart2.setOption(option2);
            gwChart3.setOption(option3);
            gwChart4.setOption(option, true);
            gwChart5.setOption(option5);
        });
    };

    var MQdate = [];
    var MQdata1 = [];
    var MQdata2 = [];

    /**
     * MQ网络折线图X轴数据
     * @param shift
     */
    function addMQNetworkDate() {
        intervalTime = $scope.gwTime;
        var today = new Date();
        for(var i = 4; i >= 1; i--){
            var intervalDate = new Date(today.getTime() - $scope.gwTime * i );
            MQdate.push(intervalDate.getHours() + ":" + intervalDate.getMinutes() + ":" + intervalDate.getSeconds());
        }
        MQdate.push(today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds());
    }

    /**
     * MQ系统监控
     */
    var mqChart1 = echarts.init(document.getElementById('MQmain1'));
    var mqChart2 = echarts.init(document.getElementById('MQmain2'));
    var mqChart3 = echarts.init(document.getElementById('MQmain3'));
    var mqChart4 = echarts.init(document.getElementById('MQmain4'));
    var mqChart5 = echarts.init(document.getElementById('MQmain5'));
    function getMQSystemService() {
        sysMonitorService.getMQSystemService({
            action: 'getMQSystemService'
        }, function (data) {
            var homeDiskArrays = new Array();
            var rootDiskArrays = new Array();
            var memArrays = new Array();
            addMQNetworkDate();
            if (data.message != null) {
                if( 'undefined' != typeof(data.message.CPU)){
                    var cpuJson = JSON.parse(data.message.CPU.message);
                    $scope.MQcpuCombined = cpuJson[0].combined;
                }else{
                    $scope.MQcpuCombined = 0;
                }

                if( 'undefined' != typeof(data.message.Memory)){
                    var MQmemUserd =  JSON.parse(data.message.Memory.message).used.toFixed(2);
                    var MQmemFree = JSON.parse(data.message.Memory.message).free.toFixed(2);
                    memArrays[0] ={
                        value : MQmemUserd,
                        name : '已用'
                    }
                    memArrays[1] ={
                        value : MQmemFree,
                        name : '空闲'
                    }
                }
                else{
                    memArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                }

                if( 'undefined' != typeof(data.message.Disk)){
                    var diskObj = JSON.parse(data.message.Disk.message);
                    for(var key in diskObj){
                        if(key == '/home'){
                            homeDiskArrays[0] ={
                                value : diskObj[key],
                                name : '已用'
                            }
                            homeDiskArrays[1] ={
                                value : 100 - diskObj[key],
                                name : '空闲'
                            }
                        }
                        if(key == '/'){
                            rootDiskArrays[0] ={
                                value : diskObj[key],
                                name : '已用'
                            }
                            rootDiskArrays[1] ={
                                value : 100 - diskObj[key],
                                name : '空闲'
                            }
                        }
                    }
                }
                else{
                    homeDiskArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                    rootDiskArrays[0] ={
                        value : 0,
                        name : '无数据'
                    }
                }

                if( 'undefined' != typeof(data.message.Network)){
                    var rxJson = JSON.parse(data.message.Network.message).rx;
                    var txJson = JSON.parse(data.message.Network.message).tx;
                    for(var k=0; k<rxJson.length; k++){
                        MQdata1.push((rxJson[k] /1024 ).toFixed(4));
                        MQdata2.push((txJson[k] /1024 ).toFixed(4));
                    }
                }
                else{
                    for(var k=0; k<5; k++){
                        MQdata1.push(0);
                        MQdata2.push(0);
                    }
                }
            } else {
                $scope.MQcpuCombined = 0;
                memArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                homeDiskArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                rootDiskArrays[0] ={
                    value : 0,
                    name : '无数据'
                }
                for(var k=0; k<5; k++){
                    MQdata1.push(0);
                    MQdata2.push(0);
                }
            }

            var option = {
                title: {
                    text: '网络监控',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder'
                    }
                },
                //设置图表大小
                grid: {
                    x: 130,
                    y: 60
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: MQdate
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        //animation: false
                    }
                },
                yAxis: {
                    boundaryGap: [0, '80%'],
                    axisLabel: {
                        formatter: '{value}（KB）'
                    },
                    splitNumber: 5
                },
                series: [
                    {
                        name: '接收',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: MQdata1
                    },
                    {
                        name: '发送',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: MQdata2
                    }
                ]
            };
            var option1 = {
                title: {
                    text: 'CPU',
                    x: '39%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    feature: {}
                },
                series: [
                    {
                        name: 'CPU指标',
                        type: 'gauge',
                        //占用比例样式
                        detail: {
                            formatter: '{value}%',
                            textStyle: {
                                color: 'auto',
                                fontSize: 15
                            }
                        },
                        data: [{
                            value: Math.round($scope.MQcpuCombined),
                            name: '占用率'
                        }
                        ],
                        //坐标标签样式
                        axisLabel: {
                            show: false,
                            formatter: null,
                            textStyle: {
                                color: 'auto'
                            }
                        },

                        //指针
                        pointer: {
                            length: '70%',
                            width: 4,
                            color: 'auto'
                        }
                    }
                ]
            };
            var option2 = {
                title: {
                    text: '内存',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: memArrays
                },
                series: [
                    {
                        name: '内存容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: memArrays
                    }
                ]
            };
            var option3 = {
                title: {
                    text: '硬盘(/home)',
                    x: '15%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{c}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: homeDiskArrays
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: homeDiskArrays
                    }
                ]
            };
            var option5 = {
                title: {
                    text: '硬盘(/)',
                    x: '30%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{c}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: rootDiskArrays
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: rootDiskArrays
                    }
                ]
            };

            // 为echarts对象加载数据
            mqChart1.setOption(option1);
            mqChart2.setOption(option2);
            mqChart3.setOption(option3);
            mqChart4.setOption(option, true);
            mqChart5.setOption(option5);
        });
    };

    setInterval(function () {
        for (var i = 0; i < 5; i++) {
            date.shift();
            data1.shift();
            data2.shift();
            MQdate.shift();
            MQdata1.shift();
            MQdata2.shift();
        }
        getGWSystemService();
        getMQSystemService();
    }, intervalTime);

    getGWSystemService();
    getMQSystemService();

});
